package cn.blossoming.controller.web;

import cn.blossoming.controller.vo.ApiResult;
import cn.blossoming.controller.vo.video.VideoCommentList;
import cn.blossoming.controller.vo.video.VideoListVo;
import cn.blossoming.controller.vo.video.VideoVo;
import cn.blossoming.service.dto.Video;
import cn.blossoming.service.dto.VideoComment;
import cn.blossoming.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/videos", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class VideoController extends BaseController {
    @Autowired
    private VideoService videoService;
    //video
    //private static final String VIDEO_PATH_FORMAT="%s/%s/"
    //@PostMapping(path = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    //public ApiResult<?> uploadVideo(@RequestParam(name = "file") MultipartFile file){
    //    String filename = file.getOriginalFilename();
    //    String path=
    //    OssUtils.uploadOss()
    //}

    /**
     * 查询最新视频列表
     * @param page 页数
     * @param PrePage 每页条数
     */
    @GetMapping(path = "/")
    public ApiResult<VideoListVo> getVideoList(@RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                               @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<Video> videoList = videoService.findPage(new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        VideoListVo vo = new VideoListVo();
        vo.setVideoList(videoList);
        return ApiResult.builder(vo);
    }

    /**
     * 查询视频详情
     * @param id 视频id
     */
    @GetMapping(path = "/{id}")
    public ApiResult<VideoVo> getVideoById(@PathVariable int id) {
        Video video = videoService.getVideoById(id);
        VideoVo vo = new VideoVo();
        vo.setVideo(video);
        return ApiResult.builder(vo);
    }

    /**
     * 视频点赞
     * @param id 视频id
     * @return
     */
    @PostMapping(path = "/{id}/praise")
    public ApiResult<VideoVo> videoPraise(@PathVariable int id) {
        Video video = videoService.praiseVideo(id);
        VideoVo videoVo = new VideoVo();
        videoVo.setVideo(video);
        return ApiResult.builder(videoVo);
    }

    /**
     * 获取视频评论
     * @param id 视频id
     * @param page 页数
     * @param PrePage 每页条数
     * @return
     */
    @GetMapping(path = "/{id}/comment")
    public ApiResult<VideoCommentList> videoComment(@PathVariable int id,
                                                    @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                                    @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<VideoComment> comments = videoService.getCommentsByVideoId(id, new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        VideoCommentList commentList = new VideoCommentList();
        commentList.setVideoCommentList(comments);
        return ApiResult.builder(commentList);
    }

    /**
     * 获取评论的评论
     * @param id
     * @param page
     * @param PrePage
     * @return
     */
    @GetMapping(path = "/comment/{id}")
    public ApiResult<VideoCommentList> tipsCommentComment(@PathVariable int id,
                                                            @RequestParam(name = "page", required = false, defaultValue = "0") int page,
                                                            @RequestParam(name = "per_page", required = false, defaultValue = "10") int PrePage) {
        List<VideoComment> comments = videoService.getVideoCommentCommentByCommentId(id, new PageRequest(page, PrePage, new Sort(Sort.Direction.DESC, "id")));
        VideoCommentList commentList = new VideoCommentList();
        commentList.setVideoCommentList(comments);
        return ApiResult.builder(commentList);
    }

    /**
     * 评论点赞
     * @param id
     * @return
     */
    @PostMapping(path = "/praise/comment/{id}")
    public ApiResult<?> videoCommentPraise(@PathVariable int id) {
        videoService.videoCommentPraise(id);
        return ApiResult.builder(null);
    }

    /**
     * 添加评论
     * @param id 视频id
     * @param comment 评论内容
     * @return
     */
    @PostMapping(path = "/{id}/comment")
    public ApiResult<?> addVideoComment(@PathVariable int id, @RequestBody VideoComment comment) {
        videoService.addVideoComment(id, comment.getContent());
        return ApiResult.builder(null);
    }

    /**
     * 添加评论的评论
     * @param id 评论id
     * @param comment 内容+视频id
     * @return
     */
    @PostMapping(path = "/comment/{id}")
    public ApiResult<?> addVideoCommentComment(@PathVariable int id, @RequestBody VideoComment comment) {
        videoService.addVideoCommentComment(id, comment.getContent(), comment.getVideoId());
        return ApiResult.builder(null);
    }

}
